var roleTable;
var btnAdvSearchflg= false;
var hasAdminRole;

//初始化iCheck组件
turn_on_iCheck = function(domElement) {
    return $('input[type="checkbox"],input[type="radio"]', domElement).iCheck({
        checkboxClass : 'icheckbox_flat-red',
        radioClass : 'iradio_flat-red'
    });
};

jQuery(document).ready(function(){
	roleTable = $('#roleTable').DataTable({
	    "processing": true,
        "serverSide": true,
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "dom": '<"bottom"rtflp>',
        "searching": false,
        "pagingType": "full_numbers",
        "deferRender": true,
        "ajax": {
            "url": "/role/roleList.action",
            "data": function ( d ) {
            	 if ($("#advSearchPanel").is(":hidden")) {
                     d.quickSearch = encodeURI($('#quickSearch').val());
                     hasAdminRole =d.hasAdminRole;
                 } else {
                     // 高级查询条件
                     var grolename = encodeURI($('#grolename').val());
                     var isAdminrole =encodeURI($('#isAdminrole').val());
                     var roleEnable =encodeURI($('#roleEnable').val());
                     var advData = {
                         "roleName": grolename,
                         "isAdminrole": isAdminrole,
                         "roleEnable": roleEnable
                     };
                     d.formJson = JSON.stringify(advData);
                 }
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [{
            	"data": "chk"
        	}, {
                "data": "roleName"
            }, {
                "data": "roleDescription"
            }, {
                "data": "userNumbers"
            }, {
                "data": "isAdminrole"
            }, {
                "data": "roleEnable"
            }
        ],
        "columnDefs": [{
	        	"orderable":false,
	        	"targets":[0]
        	},{
                "orderable":false,
                "targets":[2]
            },{
                "orderable":false,
                "targets":[5]
            }
        ],
        "order": [[ 3, "desc" ],[ 1, "asc" ]]
    }).on('draw.dt',  function() {
       turn_on_iCheck($("#roleTable"));
        $("#chkAll").on("ifChanged", function() {
            if ($("#chkAll").is(":checked")) {
                $("#roleTable").find(".icheck").iCheck("check");
            } else {
                $("#roleTable").find(".icheck").iCheck("uncheck");
            }
        });
    });
});

$("#unitSelect").click(function(){
    if(hasAdminRole != "false"){
        if(window.top.window.document.getElementById("selIsAdminRole").value=="是"){
            window.top.window.returnCustomModalDialog();
            var editDialog = window.top.window.borrowCustomModalDialog($("#queryUnitSelectCheck"));
            editDialog.modal({show:true, backdrop:'static'});
        }
    }
});
//重置按钮
$("#advSearchReset").click(function(){
	$('#grolename').val('');
	$('#isAdminrole').val('');
});
//低级查询按钮
$("#btnQuickSearch").click(function () {
    roleTable.draw();
});
//高级查询按钮
$("#advSearchSubmit").click(function () {
    setHidValue("frmAdvSearch");
    roleTable.draw();
    $("#advSearchPanel").hide();
    $("#btnAdvSearch").children("i:eq(0)").removeClass("fa-angle-double-up");
    $("#btnAdvSearch").children("i:eq(0)").addClass("fa-angle-double-down");
});

//高级查询隐藏
$("#advSearchPanel").hide();
$("#btnAdvSearch").click(function () {
	btnAdvSearchflg = true;
	if ($("#advSearchPanel").is(":hidden")) {
		$("#advSearchPanel").show();
		$("#btnQuickSearch").hide();
		$(this).children("i:eq(0)").removeClass("fa-angle-double-down");
		$(this).children("i:eq(0)").addClass("fa-angle-double-up");
	} else {
		$("#advSearchPanel").hide();
		 $("#btnQuickSearch").show();
		$(this).children("i:eq(0)").removeClass("fa-angle-double-up");
		$(this).children("i:eq(0)").addClass("fa-angle-double-down");
	}
});

$("#advSearchPanel").click(function () {
    btnAdvSearchflg = true;
})

$("body").click(function(e){ 
    if(btnAdvSearchflg==true){
        btnAdvSearchflg = false;
    } else {
        $("#advSearchPanel").hide();
        $("#btnAdvSearch").children("i:eq(0)").removeClass("fa-angle-double-up");
        $("#btnAdvSearch").children("i:eq(0)").addClass("fa-angle-double-down");    
    }
});

  // 搜索框的回车事件
$('#quickSearch').keydown(function(e){
    if (e.keyCode == 13 && $('#btnQuickSearch').is(":visible")) {
        $('#btnQuickSearch').click();
    }
});

$('#creatRole').on('hide.bs.modal', function () {
	// 父窗体归还本页的编辑对话框
	window.top.window.returnCustomModalDialog();
	ClearInput();
	$("#txtRoleName").val("");
	$("#txtRoleDesc").val("");
	$("#txtMax").val("-1");
	$("#txtRoleUsers").val("");
	$("#selIsAdminRole").val("否");
	$("#txtRoleUnits").val("");	
	$("#txtRoleUnitsValue").val("");
	$("#roleId").val("");
});
$('#showItemList').on('hide.bs.modal', function () {
	// 父窗体归还本页的编辑对话框
	window.top.window.returnCustomModalDialog();
	var editDialog = window.top.window.borrowCustomModalDialog($("#creatRole"));
})

//添加
function addRole(){
    $.ajax({
        url : "/role/hasAdminRole.action",
        type : "post",
        dataType : "json",
        async: false,
        success : function(data) {
            hasAdminRole = data.hasAdminRole;
            if (data.hasAdminRole == "false") {
                $("#selIsAdminRole").attr('disabled',true);
                $("#txtRoleUnits").attr('disabled',true);
                $("#unitSelect").attr('disabled',true);
            }
        },
    });
    $("#createTitle").html("新建角色");
    $("#selIsAdminRole").attr('disabled',false);
	var editDialog = window.top.window.borrowCustomModalDialog($("#creatRole"));
	$("#txtRoleUsers").val("");
	editDialog.modal({show:true, backdrop:'static'});
} 

//保存按钮
$('#roleSave').click(function(){
	// 输入有效性验证
	$("detailForm").validate({
		debug:true,
        errorPlacement: function(error, element)
        {
            error.insertAfter(element);
        }
    });
	insertRole();
	roleTable.draw();
});
//删除
var roleIdssum1 ="";
function deleteRole(){
	roleIdssum1 = "";
	var temp = 1;
	$("input[type='checkbox']:checked").each(function(k,v){
		if(roleIdssum1==""){
			if(this.getAttribute('roleid')=="adminRole"){
				temp = 2;
				return;
			}
			
			roleIdssum1=this.getAttribute('roleid');
		}else if(roleIdssum1!=""){
			if(this.getAttribute('roleid')=="adminRole"){
			  temp =2;
			  return;
			}
			roleIdssum1+=","+this.getAttribute('roleid');
			
		}
	});
    if(temp !=1)
    	{
        window.top.window.showModalAlert("adminRole不能删除,请重新选择");
		return;
    	
    	}
    
	if (roleIdssum1 == null || roleIdssum1==undefined || roleIdssum1=="" || roleIdssum1 == ","){
	    window.top.window.showModalAlert("请选择一个角色");
		return;
	}
	window.top.window.showModalConfirm("确定要删除角色吗？", delRole);
}
//删除按钮
function delRole(){
	var sURL = "/role/deleteRole.action";
	var loading = window.top.window.getIndexBusyOverlay();
	 $.ajax({
		url : sURL,
		type : "post",
		dataType : "json",
		
//		async: false,
		data : {
			roleIds : roleIdssum1
		},
		beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
		success : function(data) {
			 if (data.result == "success") {
				 loading.remove();
	            	window.top.window.showScoMessage('ok', '删除成功');
	            	roleTable.draw();
	            } else{
	            	window.top.window.showModalAlert(data.msg);
	            }
		},
		error:function(XMLHttpRequest, textStatus) {
			loading.remove();
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showModalAlert(result.errorObject.errorText);
            }
        }
	});
//	roleTable.draw();
}

/**
 * 添加角色
 * @returns
 */
function insertRole() {
	// 父窗体归还本页的编辑对话框
	window.top.window.returnCustomModalDialog();
	var roleName = $("#txtRoleName").val();
    var roleDescription = $("#txtRoleDesc").val();
    var txtMax = $("#txtMax").val();
    var txtRoleUsers =$("#txtRoleUsersValue").val();
    var txtRoleUnitsValue = $("#txtRoleUnitsValue").val();
    var isAdminrole = $("#selIsAdminRole").val();
    var txtRoleUnits = $("#txtRoleUnits").val();
  //验证-1,0,正整数
    $.validator.addMethod("integerValidate", function(value, element) {
        var regx = /^[0-9]\d*$/; 
        return (regx.test(value) || value=="-1");
    }, "最大人数必填，小于10位整数，-1~999999999");
 
   /////////////////////////////////////// 
    if(!$("#roleDetailForm").valid()) {
    	// 父窗体借用本页的编辑对话框
    	window.top.window.borrowCustomModalDialog($("#creatRole"));
		return false;
	}
    if($("#roleId").val()!= null&&$("#roleId").val()!=''){
        var Rid =$("#roleId").val();
        var data = {
            	"roleName": roleName,
            	"roleDescri": roleDescription,
                "userNumbers": txtMax,
                "isAdminrole": isAdminrole,
                "txtRoleUsers":txtRoleUsers,
                "txtRoleUnits": txtRoleUnits,
                "txtRoleUnitsValue": txtRoleUnitsValue,
                "roleId": Rid
            };
        var loading = window.top.window.getIndexBusyOverlay();
       	 $.ajax({
   	        type:"POST",
   	        url:"/role/updateRole.action",
   	        data:JSON.stringify(data),
   	        contentType: "application/json;charset=utf-8",
   	        dataType: "json",
//   	        async: false, // 同步
   	        beforeSend:function(){
                if(loading) {
                	loading.settext("正在处理中，请稍后...");
                }
            },
   	        success:function(data) {
   	        	loading.remove();
   	            if (data.result == "success") {
   	            	$('#creatRole').modal('hide');
   	            	// 清空
   	                $("#txtRoleName").val("");
       	            $("#txtRoleDesc").val("");
       	            $("#txtMax").val("-1");
       	            $("#txtRoleUsers").val("");
       	            $("#selIsAdminRole").val("否");
       	            $("#txtRoleUnits").val(""); 
       	            $("#txtRoleUnitsValue").val("");
       	            $("#roleId").val("");
   	            	window.top.window.showScoMessage('ok', '修改成功');
   	            	roleTable.draw();
   	            } else{
   	            	window.top.window.showModalAlert(data.msg);
   	            }
   	        },
   	        error:function(XMLHttpRequest, textStatus) {
   	        	loading.remove();
   	            if (XMLHttpRequest.status == 500) {
   	                var result = eval("(" + XMLHttpRequest.responseText + ")");
   	                window.top.window.showModalAlert(result.errorObject.errorText);
   	            }
   	        }
   	    });
    } else {
	   var data = {
		    	"roleName": roleName,
		    	"roleDescri": roleDescription,
		        "userNumbers": txtMax,
		        "isAdminrole": isAdminrole,
		        "txtRoleUsers":txtRoleUsers,
//		        "txtRoleUnits": txtRoleUnits,
		        "txtRoleUnitsValue": txtRoleUnitsValue,
		        "txtRoleUnits": txtRoleUnitsValue
		    };
	   var loading = window.top.window.getIndexBusyOverlay();
	   //新建角色
	   $.ajax({
	       type:"POST",
	       url:"/role/addRole.action",
	       data:JSON.stringify(data),
	       contentType: "application/json;charset=utf-8",
	       dataType: "json",
//	       async: false, // 同步
	       beforeSend:function(){
	            if(loading) {
	            	loading.settext("正在处理中，请稍后...");
	            }
	        },
	       success:function(data) {
	    	   loading.remove();
	           if (data.result == "success") {
	           	 $('#creatRole').modal('hide');
	           	 // 清空
	             $("#txtRoleName").val("");
	             $("#txtRoleDesc").val("");
	             $("#txtMax").val("-1");
	             $("#txtRoleUsers").val("");
	             $("#selIsAdminRole").val("否");
	             $("#txtRoleUnits").val(""); 
	             $("#txtRoleUnitsValue").val("");
	             $("#roleId").val("");
	           	 window.top.window.showScoMessage('ok', '添加成功');
	           	roleTable.draw();
	           } else {
                 window.top.window.showModalAlert(data.msg);
	           }
	       },
	       error:function(XMLHttpRequest, textStatus) {
	    	   loading.remove();
	           if (XMLHttpRequest.status == 500) {
	               var result = eval("(" + XMLHttpRequest.responseText + ")");
	               window.top.window.showModalAlert(result.errorObject.errorText);
	           }
	       }
	   });
    }
    //$("#roleId").val('');
//    roleTable.draw();
}
//包含人员选择按钮
$('#manageUser').click(function () {
	manageUser();
})
//包含人员保存按钮
$('#btnSaveManger').click(function () {
	btnSaveManger();
})
//包含人员
function manageUser(i){

	$("#btnSaveManger").show();
	$("#btnSaveWork").hide();
	var txtRoleUsersID=window.top.window.document.getElementById("txtRoleUsersValue").value  //
	window.top.window.returnCustomModalDialog();
	testuser = $("#txtRoleUsersValue").val();
	$("#UserFrameCheck").attr("src","/views/system/organization/user/UserSelect.html?txtRoleUsersValue="+ txtRoleUsersID);
	var editDialog = window.top.window.borrowCustomModalDialog($("#showItemList"));
	editDialog.modal({show:true, backdrop:'static'});
}

//保存
function btnSaveManger(){
	var obj=$(window.parent.document).contents().find("#UserFrameCheck")[0].contentWindow.userSaveSelect();
	window.top.window.returnCustomModalDialog();
	$("#txtRoleUsers").val(obj.txtRoleUsers);
	$("#txtRoleUsersValue").val(obj.txtRoleUsersValue);
	var div=window.top.window.borrowCustomModalDialog($("#showItemList"));
	div.modal('hide');
	
}	
//修改用户
function updateRole(){
    
    $.ajax({
        url : "/role/hasAdminRole.action",
        type : "post",
        dataType : "json",
        async: false,
        success : function(data) {
            hasAdminRole = data.hasAdminRole;
             if (data.hasAdminRole == "false") {
                 $("#selIsAdminRole").attr('disabled',true);
                 $("#txtRoleUnits").attr('disabled',true);
                 $("#unitSelect").attr('disabled',true);
                }
        },
    });
    $("#selIsAdminRole").attr('disabled',true);
	var idString = "";
	$("input[type='checkbox']:checked").each(function(k,v){
		if(idString==""){
			if(this.getAttribute('roleid')!=null)
			idString=this.getAttribute('roleid');
		}else if(idString!=""){
			if(this.getAttribute('roleid')!=null)
			idString+=","+this.getAttribute('roleid');
		}
	});

	if (idString == null || idString==undefined || idString=="" || idString == ","){
	    window.top.window.showModalAlert("请选择角色");
		return;
	}else if(idString.split(",").length>1){
	    window.top.window.showModalAlert("只能选择一个");
		return;
	}
	var sURL = "/role/getRoleDetail.action";
	jQuery.ajax( {
		url : sURL,
		type : "post",
		dataType : "json",
		data : {
			roleId : idString
		},
		success : function(data) {
		    //////////////////////////////////////////////////////////////////
		    if(data.errorMessage == undefined){
        	    if(data.roleDetail.roleId == "adminRole"){
                    $("#selIsAdminRole").attr('disabled',true);
                }
        		$("#roleId").val(data.roleDetail.roleId);
        		$("#txtRoleName").val(data.roleDetail.roleName);
        		$("#txtRoleDesc").val(data.roleDetail.roleDescription);
        		$("#txtMax").val(data.roleDetail.userNumbers);
        		$("#txtRoleUsers").val(data.roleUsersVALUE);
        		$("#txtRoleUsersValue").val(data.roleUsersKEY);
        		$("#selIsAdminRole").val(data.roleDetail.isAdminrole);
        		$("#txtRoleUnits").val(data.roleManageUnitsVALUE);
        		$("#txtRoleUnitsValue").val(data.roleManageUnitsKEY);
        		$("#createTitle").html("修改角色");
        		var editDialog = window.top.window.borrowCustomModalDialog($("#creatRole"));
        		editDialog.modal({show:true, backdrop:'static'});
		    } else {
                if (data.errorMessage == "session timeout"){
                    window.location.href = "/login.jsp";
                }
                else{
                    window.top.window.showModalAlert(data.errorMessage);
                }
		    }
		   //////////////////////////////////////////////////////////////////////////////////
		},
		error : function(XMLHttpRequest,textStatus,errorThrown){
		    window.top.window.showModalAlert("操作失败，可能是网络原因");
		}
	});
}

//组织单元选择窗口返回父页面
$('#queryUnitSelectCheck').on('hide.bs.modal', function () {
	// 父窗体归还本页的编辑对话框
	window.top.window.returnCustomModalDialog();
	var editDialog = window.top.window.borrowCustomModalDialog($("#creatRole"));
})

//组织单元保存
function unitSaveCheck(){
	var o=$(window.parent.document).contents().find("#UnitFrameCheck")[0].contentWindow.unitSaveSelect();
	window.top.window.returnCustomModalDialog();
	var RoleUnitsValueLen = o.itemIds;
	$("#txtRoleUnits").val(o.itemTexts);
	$("#txtRoleUnitsValue").val(RoleUnitsValueLen);
	var div=window.top.window.borrowCustomModalDialog($("#queryUnitSelectCheck"));
	div.modal('hide');
}

// 全选/单选
$('#roleTable').checkAll({
    checkbox: '#chkAll'
});

//组织单元保存按钮
$("#unitSaveCheck").click(function(){
	unitSaveCheck();
});

//是否按钮
$("#selIsAdminRole").on('change',function(){
	if(window.top.window.document.getElementById("selIsAdminRole").value=="否"){
		window.top.window.document.getElementById("txtRoleUnits").value="";
		window.top.window.document.getElementById("txtRoleUnitsValue").value="";
	}
	
});
//有效性验证
function checkValidate(){
	if(!jQuery("#detailForm").valid())return false;
    return true;
}
function ClearInput() {  
    //清空输入域  
   var validator = $("#roleDetailForm").validate({  
        submitHandler: function (form) {  
            form.submit();  
        }  
    });  
    validator.resetForm();  
    $('#roleDetailForm .state-error').removeClass('state-error');
    $('#roleDetailForm .state-success').removeClass('state-success');
} 

//起停用角色
function startOrStopUsing(){
    if($("input[type='checkbox']:checked").length!=1){
        return window.top.window.showModalAlert("请选择一个角色");
    }
    if($("input[type='checkbox']:checked").length > 1){
        return window.top.window.showModalAlert("只能选择一个角色");
    }
    var idString = ""; var isLocked="";
    $("input[type='checkbox']:checked").each(function(k,v){
        isLocked=$($(this).parent().parent().parent().children()[5]).html();
        idString=this.getAttribute('roleid');
    });
    var selectedRoleID=idString;
    selectedRoleID=selectedRoleID;
    isLocked=isLocked;
    $("#selectedRoleID").val(selectedRoleID);
    $("#isLocked").val(isLocked);
    var showInformation = "";
    if(isLocked == "是") {
        showInformation ="确定停用角色？"
    }else{
        showInformation ="确定启用角色？"
    }
    window.top.window.showModalConfirm(showInformation, changeflg);
}
//启用、停用角色按钮
function changeflg(){
	var loading = window.top.window.getIndexBusyOverlay();
    var selectedRoleID=$("#selectedRoleID").val();
    var isLocked=$("#isLocked").val();
    var sURL = "/role/enable.action";
    jQuery.ajax( {
        url : sURL,
//        async: false,
        type : "post",
        dataType : "json",
        data : {
            roleid : selectedRoleID,
            roleEnable : isLocked
        },
        beforeSend:function(){
            if(loading) {
            	loading.settext("正在处理中，请稍后...");
            }
        },
        success : function(data) {
        	loading.remove();
            if(data.errorMessage==null || data.errorMessage==undefined){
                roleTable.draw();
            } else {
                if (data.errorMessage == "session timeout")
                    window.location.href = "/login.jsp";
                else
                    window.top.window.showModalAlert(data.errorMessage);
            }
        },
        error : function(XMLHttpRequest,textStatus,errorThrown){
        	loading.remove();
            window.top.window.showModalAlert("操作失败，可能是网络原因");
        }
    });
}